
#include "kb.h"                 // keybaord event type 
#include "touch_algorithm.h"    // touch event type 
#include "custom_event.h"
#include "gui.h"                // GUI header

#include "views.h"              // include all views 
#include "views_util.h"

#include "log.h"                // LOG file 
#define LOG     LOG_INFO0


#include "xapp_clock.h"
#include "xapp_timer.h"

enum tagTimerSet
{
    tagTimerSetStart,
    tagTimerSetHourTouched,
    tagTimerSetMinuteTouched,
    tagTimerSetHourClick,
    tagTimerSetMinuteClick,
    tagTimerSetDisable,
    tagTimerSetBack,
};

enum flagTimerSet
{
    flagTimerSetMSB,
    flagTimerSetLSB,
}; 

// declare function 
GUI_VIEW_FUNC_DECLARE_ALL;
static void local_view_event_handler( uint32_t tag );

#define ITEM_HEIGHT (60)
#define PICK_HEIGHT (ITEM_HEIGHT*3)
#define PICK_WIDTH  (SIZE_SCALE(40))
#define Y_OFFSET    (SIZE_SCALE(35))


const static ui_widgets_type_t wglTimerCustom/*1. modify name*/[] = 
{
    // background 
    [0] = uiCanvas(0, 0, SIZE_SCALE(240), SIZE_SCALE(240), COLOR_BLACK ), // uiPictureBox( IQW_VIEW_IMG_Background_240_240_03, 0, 0, 0, 240, 240 ),

    // line 
    [1] = uiCanvas(SIZE_SCALE(30), SIZE_SCALE(78), SIZE_SCALE(240 - 30 * 2), SIZE_SCALE(1), COLOR_GRAY ),
    // line2 
    [2] = uiCanvas( SIZE_SCALE(30), SIZE_SCALE(118) , SIZE_SCALE(240 - 30 * 2), SIZE_SCALE(1), COLOR_GRAY ), 
    
    // time set(picker) hour, center Y = (y * 2 + h ) / 2 - height / 2 h = 3 * 20 + 2 *pitch 
    [3] = uiPicker( SIZE_SCALE(70), Y_OFFSET, PICK_WIDTH, PICK_HEIGHT, COLOR_WHITE, ALIGN_MIDDLE_CENTOR, ENCODING_ASCII, FONT_ASCII_2_40_57, ITEM_HEIGHT-57 ),
    [4] = uiPicker( SIZE_SCALE(140),Y_OFFSET, PICK_WIDTH, PICK_HEIGHT, COLOR_WHITE, ALIGN_MIDDLE_CENTOR, ENCODING_ASCII, FONT_ASCII_2_40_57, ITEM_HEIGHT-57 ), 
    
    [5] = uiLabel( SIZE_SCALE(116), SIZE_SCALE(76), 40, 57, COLOR_BLACK, COLOR_WHITE, ALIGN_MIDDLE_CENTOR, FILL_NONE, ENCODING_ASCII, FONT_ASCII_2_40_57, ":", 1 ),

    // mask color 
    [6] = uiColorReplacer( SIZE_SCALE(70), Y_OFFSET+ITEM_HEIGHT*1+2, PICK_WIDTH, ITEM_HEIGHT-4, COLOR_WHITE, COLOR_ORANGE ),
    // mask color 
    [7] = uiColorReplacer( SIZE_SCALE(140), Y_OFFSET+ITEM_HEIGHT*1+2,PICK_WIDTH, ITEM_HEIGHT-4, COLOR_WHITE, COLOR_ORANGE ), 

    // button start
    [8] = uiPictureBox( IMG_Setting_Stopwatch_Timer_Button_102_054_04, 1, SIZE_SCALE(86), SIZE_SCALE(180), SIZE_SCALE(68), SIZE_SCALE(36) ),

//    [9]  = uiPictureBox( IMG_Hrs_Dotted_Line_168_003_01, 0, SIZE_SCALE(70), SIZE_SCALE(78), 168, 3 ),
//    [10] = uiPictureBox( IMG_Hrs_Dotted_Line_168_003_01, 0, SIZE_SCALE(70), SIZE_SCALE(118), 168, 3 ),
    
};

//const static ui_widgets_type_t wglTimerSelectTouch[] = 
//{
//    [0] = uiTouch( 20, 160, 220, 240 - 160, tagTimerSetStart ), 
//};

const static ui_widgets_type_t wglTimerSetPickerTouch[] = 
{
   [0] = uiTouch( 0, SIZE_SCALE(50), SIZE_SCALE(120), SIZE_SCALE(114), tagTimerSetHourTouched ),
   [1] = uiTouch( SIZE_SCALE(120), SIZE_SCALE(50), SIZE_SCALE(120), SIZE_SCALE(114), tagTimerSetMinuteTouched ), 
};
const static ui_widgets_type_t wglTimerSetPickerClick[] = 
{
   [0] = uiTouch( 0, SIZE_SCALE(50), SIZE_SCALE(120), SIZE_SCALE(114), tagTimerSetHourClick ),
   [1] = uiTouch( SIZE_SCALE(120), SIZE_SCALE(50), SIZE_SCALE(120), SIZE_SCALE(114), tagTimerSetMinuteClick ), 
   [2] = uiTouch( 0, SIZE_SCALE(180), GUI_LCD_RES_H, SIZE_SCALE(60), tagTimerSetStart ),
};


GUI_VIEW_DEF( viewTools_TimerCustom ) = 
{
    // name 
    .name = "Tools+Timer",
    
    // init 
    .init = initialize,
    .display_func = display_callback_func,
    .refresh_func_check = display_refresh_func,
    .destroy_func = on_destroy_func,
    .set_parent_func = on_set_parent_func, 
    
    // input 
    .on_touch = on_touch,
    .on_button = on_button,
    .on_event_update = on_event_update,     
    
    .pWidgetlist =  (ui_widgets_type_t*)wglTimerCustom /* 3. fill with widgets list name */,
    .widget_cnt = WIDGET_COUNT( wglTimerCustom ), /* 3. fill with widgets list name */

    .flag = GUI_VIEW_FLAG_FUNC
};

static gui_fling_t *fling_msb, *fling_lsb;
static uint8_t msb, lsb;

/* 当前view初始化
// 需要指定前使用的控件组
// 自动更新间隔，不使用自动更新间隔则interval = 0

*/
static void initialize(gui_menu_t * menu, void* pvParameter)
{
    menu->interval = 100;
    menu->lifetime = 0; 

    // 获取公用的滚动管理器
    fling_msb = gui_fling_get();
    fling_lsb = gui_fling_get(); 

    gui_fling_init_t msb_fling_config = GUI_FLING_CONFIG( SIZE_SCALE(70), Y_OFFSET, PICK_WIDTH, PICK_HEIGHT, true, 0, 0, ITEM_HEIGHT, 100, fling_lsb );
    gui_fling_init( fling_msb, &msb_fling_config );
    gui_fling_init_t lsb_fling_config = GUI_FLING_CONFIG( SIZE_SCALE(140), Y_OFFSET, PICK_WIDTH, PICK_HEIGHT, false, 0, 0, ITEM_HEIGHT, 100, NULL );
    gui_fling_init( fling_lsb, &lsb_fling_config );
}
static void on_set_parent_func(gui_menu_t * menu, const gui_view_t * parent)
{
   p_this_parent = parent;
}
/* 销毁函数
// 在销毁前处理当前信息，比如不允许销毁时，将当前界面状态保存为background。
// true:  允许销毁
// false: 不允许自动销毁，则GUI只关闭当前显示，输入事件仍由当前view进行处理。
          那么view判断当前状态为background时，作出对应background下的按键、触摸等事件反馈
*/
static uint32_t on_destroy_func(gui_menu_t * menu)
{
        
    return GUI_DESTROY_RET_DEFAULT; 
}

/* 显示回调
   重新指定参数, 用来获取当前界面的控件组
   ** 当前界面有多个控件组时，应该有条件确定使用哪一组控件组 ** 
*/ 
static void display_callback_func(gui_menu_t * menu)
{
    ui_property_t prop;
    ui_widgets_type_t * widgets = menu->widgets;
    
    if( IS_REQ_SRC_CREATE( menu->source) )
    {
        PROP_UPDATE_NULL_AT_NO( 5 );

        // update part 
    }

    PROP_ASSIGN_PICKER_OFFS( prop, gui_assets_get_hour_items, 24, fling_msb->roll_offset_total );
    PROP_UPDATE_AT_NO( 3 ); 
    PROP_ASSIGN_PICKER_OFFS( prop, gui_assets_get_minute_items, 60, fling_lsb->roll_offset_total );
    PROP_UPDATE_AT_NO( 4 ); 
    msb = GET_PROP_PICKER_IDX( widgets[3] ); 
    lsb = GET_PROP_PICKER_IDX( widgets[4] );

//    WGT_ENABLE_RESET( widgets[1] );
//    WGT_ENABLE_RESET( widgets[2] );
    
    
    
//    // 取消颜色替换，代表选中
//    if( FLAG_GETB( flagTimerSetMSB) )
//    {
//        WGT_ENABLE_RESET( widgets[6] );
//    }
//    else 
//    {
//        WGT_ENABLE_SET( widgets[6] );
//    }
//    if( FLAG_GETB(flagTimerSetLSB) )
//    {
//        WGT_ENABLE_RESET( widgets[7] );
//    }
//    else 
//    {
//        WGT_ENABLE_SET( widgets[7] );
//    }

}

/* 刷新显示回调
   根据当前状态，重新绘制指定的控件组 
*/
static int display_refresh_func(gui_menu_t * menu)
{
    return 1;
}

static void on_touch(void* pvParameters, gui_menu_t * menu)
{
    tp_algo_evt_t* touch = (tp_algo_evt_t*)pvParameters;

    switch( touch->type )
    {
        case TP_ALGO_EVT_TYPE_DOWN:
        if( VIEW_CHECK_TOUCH( wglTimerSetPickerTouch, touch->x, touch->y, local_view_event_handler ) == false )
        {
            //local_view_event_handler( tagTimerSetDisable );
        }
        break;

        case TP_ALGO_EVT_TYPE_UP:
        gui_fling_on_touch_release( fling_msb );
        break;

        case TP_ALGO_EVT_TYPE_HOLD:
        if( touch->deltaY != 0 )
        {
            if( FLAG_GETB(flagTimerSetMSB) )
            {
                gui_fling_set_roll( fling_msb, touch->deltaY );
            }
            if( FLAG_GETB(flagTimerSetLSB) )
            {
                gui_fling_set_roll( fling_lsb, touch->deltaY );
            }
        }
        break;

        case TP_ALGO_EVT_TYPE_CLICK:
        if( VIEW_CHECK_TOUCH( wglTimerSetPickerClick, touch->x, touch->y, local_view_event_handler ) == false )
        {
            local_view_event_handler( tagTimerSetDisable );
        }
        break;
        
        case TP_ALGO_EVT_TYPE_SLIDE_D:
        if( FLAG_GETB(flagTimerSetMSB) )
        {
            gui_fling_switch_item( fling_msb, 3, false );
        }
        if( FLAG_GETB(flagTimerSetLSB) )
        {
            gui_fling_switch_item( fling_lsb, 3, false );
        }
        break;
        
        case TP_ALGO_EVT_TYPE_SLIDE_U:
        if( FLAG_GETB(flagTimerSetMSB) )
        {
            gui_fling_switch_item( fling_msb, 3, true );
        }
        if( FLAG_GETB(flagTimerSetLSB) )
        {
            gui_fling_switch_item( fling_lsb, 3, true );
        }
        break;
        
        
        case TP_ALGO_EVT_TYPE_SLIDE_R:        
        local_view_event_handler( tagTimerSetBack );
        break;
        
        default:
        break;
    }
}
/* 界面的按键处理

*/
static void on_button(void* pvParameters, gui_menu_t * menu)
{
    kb_evt_t * kb = (kb_evt_t*)pvParameters;
    
    switch( kb->type ) 
    {
        case KB_EVT_CLICK:           
        case KB_EVT_SHORT_HOLD_READY:
        local_view_event_handler( tagTimerSetBack ); 
        break;

        default:
        break;
    }
 
}

/* 界面的自定义输入事件处理

*/
static void on_event_update(void * pvParameters, gui_menu_t * menu)
{
    custom_evt_t* custom = (custom_evt_t*)pvParameters;
    switch( custom->type )
    {
        case CUSTOM_EVT_TYPE_RAISE_WRIST:
        break;

        case CUSTOM_EVT_TYPE_DROP_WRIST:
        break;
        
        default:
        break;
    }
}

/* 界面的触摸事件
   tag 是生效的控件id, 用户根据生效的tag判断是哪个控件点击了，进行处理
*/
static void local_view_event_handler( uint32_t tag )
{
    switch( tag )
    {
        case tagTimerSetHourTouched:
        FLAG_SETB( flagTimerSetMSB );
        FLAG_RESETB( flagTimerSetLSB );
        gui_update_manual();
        break;

        case tagTimerSetMinuteTouched:
        FLAG_RESETB( flagTimerSetMSB );
        FLAG_SETB( flagTimerSetLSB );

        gui_update_manual();
        break;

        case tagTimerSetHourClick:
        FLAG_TOGGLEB( flagTimerSetMSB );
        FLAG_RESETB( flagTimerSetLSB );
        gui_update_manual();
        break;

        case tagTimerSetMinuteClick:
        FLAG_RESETB( flagTimerSetMSB );
        FLAG_TOGGLEB( flagTimerSetLSB );
        gui_update_manual();
        break;

        case tagTimerSetDisable:
        FLAG_CLEAR();
        gui_update_manual(); 
        break;
        
        case tagTimerSetStart:
        if( msb != 0 || lsb != 0 )
        {
            xapp_timer_set_time( msb, lsb );
            xapp_timer_counting_start();
            gui_view_create( &viewTools_TimerElapsed );
        }
        break;


        case tagTimerSetBack:
        {
            const xapp_timer_desc_t* p_timer_desc = xapp_timer_get();
            if( p_timer_desc->started )
            {
                gui_view_create_return( &viewAppsList );
            }
            else 
            {               
                if( p_this_parent != NULL) 
                {
                    gui_view_create_return( p_this_parent );
                }
                else 
                {
                    gui_view_create_return( &viewTools_TimerSelect );
                }
            }
        }
        break;
    }
}
